package qiyu.im.core.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import qiyu.im.core.server.constants.ImMsg;
import qiyu.im.core.server.handler.ImCoreHandlerFactoryImpl;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-10-16
 * Time: 20:09
 */
public class ImCoreServerHandler extends SimpleChannelInboundHandler {

    private ImCoreHandlerFactory factory = new ImCoreHandlerFactoryImpl();

    /**
     * 用于接收外界所有消息的处理器
     *
     * @param ctx the {@link ChannelHandlerContext} which this {@link SimpleChannelInboundHandler}
     *            belongs to
     * @param msg the message to handle
     * @throws Exception
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
        if(msg == null) {
            return;
        }
        if(msg instanceof ImMsg) {
            // 记录一下
            return;
        }
        ImMsg imMsg = (ImMsg) msg;
        // 根据不同的code去调用不同的handler
        // 我们这里采用策略模式实现
        factory.doHandler(ctx, imMsg);
    }
}
